package com.sun.androidexerciseproject.function.activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.jaredrummler.android.processes.AndroidProcesses;
import com.jaredrummler.android.processes.models.AndroidAppProcess;
import com.sun.androidexerciseproject.function.R;
import com.sun.androidexerciseproject.function.util.LogUtil;
import com.sun.androidexerciseproject.function.util.PackageInfoUtil;

import java.util.List;
import java.util.Map;

public class MetaActivity extends AppCompatActivity {
    public static final String MY_PKG_NAME = "com.unionpay";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meta);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        getMetaInfo();
        boolean ret = testAppRunning();
        LogUtil.log("ret = " + ret);
        PackageInfoUtil.getProvider(this);
        PackageInfoUtil.setAnnotationTest(PackageInfoUtil.TEST1);
    }

    private void getMetaInfo() {
        LogUtil.log("getMetaInfo");
        Map<String, String> map = PackageInfoUtil.getMetaInfo(this);
        if (!map.isEmpty()) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                LogUtil.log("key = " + entry.getKey() + " value = " + entry.getValue());
            }
        }
    }

    boolean testAppRunning() {
// Get a list of running apps
        List<AndroidAppProcess> processes = AndroidProcesses.getRunningAppProcesses();

        for (AndroidAppProcess process : processes) {
            // Get some information about the process
            String processName = process.name;
            if (process.getPackageName().equals(MY_PKG_NAME)) {
                return true;
            }
        }
        return false;
    }

}
